package com.qianli.cashmama.trade.repo.impl;

import com.alibaba.fastjson.JSON;
import com.fqgj.common.utils.StringUtils;
import com.qianli.cashmama.pay.client.enums.PaymentChannelEnum;
import com.qianli.cashmama.trade.dao.TBillRepaymentDetailDao;
import com.qianli.cashmama.trade.domain.model.TradeBillDetail;
import com.qianli.cashmama.trade.entity.TBillRepaymentDetailEntity;
import com.qianli.cashmama.trade.repo.TradeBillDetailRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/7/25
 * Time: 下午9:26
 */
@Repository
public class TradeBillDetailRepositoryImpl implements TradeBillDetailRepository {
    private static final Logger logger = LoggerFactory.getLogger(TradeBillDetailRepositoryImpl.class);

    @Autowired
    private TBillRepaymentDetailDao tBillRepaymentDetailDao;

    @Override
    public void store(TradeBillDetail tradeBillDetail) {
        String paidChannel = null;
        if (tradeBillDetail.getPaidChannel() != null) {
            paidChannel = String.valueOf(tradeBillDetail.getPaidChannel().getCode());
        } else {
            logger.error("store tradeBillDetail.getPaidChannel() is null, tradeBillDetail={}", JSON.toJSONString(tradeBillDetail));
        }
        TBillRepaymentDetailEntity detailEntity = new TBillRepaymentDetailEntity()
                .setAccountedTime(tradeBillDetail.getAccountedTime())
                .setBizNo(tradeBillDetail.getBizNo())
                .setBillNo(tradeBillDetail.getBillNo())
                .setTradeNo(tradeBillDetail.getTradeNo())
                .setBankNo(tradeBillDetail.getBankNo())
                .setPaidCapital(tradeBillDetail.getPaidCapital())
                .setDiscountCapital(tradeBillDetail.getDiscountCapital())
                .setPaidChannel(paidChannel)
                .setChannelServiceFee(tradeBillDetail.getChannelServiceFee())
                .setPayBizNo(tradeBillDetail.getPayBizNo())
                .setProductCode(tradeBillDetail.getProductCode())
                .setProductCategory(tradeBillDetail.getProductCategory())
                .setOperator(tradeBillDetail.getOperator());

//        detailEntity.setId(IdCenterUtil.getSequenceNo(TradeBizCodeEnum.BILL_REPAYMENT_DETAIL));

        tBillRepaymentDetailDao.insert(detailEntity);
    }

    /**
     * @param payBizNo
     * @return
     */
    @Override
    public TradeBillDetail load(String payBizNo) {
        if (StringUtils.isEmpty(payBizNo)) {
            return null;
        }
        TBillRepaymentDetailEntity entity = tBillRepaymentDetailDao.selectByPayBizNo(payBizNo);
        TradeBillDetail tradeBillDetail = new TradeBillDetail();
        tradeBillDetail.setAccountedTime(entity.getAccountedTime());
        tradeBillDetail.setBizNo(entity.getBizNo());
        tradeBillDetail.setOperator(entity.getOperator());
        tradeBillDetail.setProductCode(entity.getProductCode());
        tradeBillDetail.setProductCategory(entity.getProductCategory());
        tradeBillDetail.setBillNo(entity.getBillNo());
        tradeBillDetail.setTradeNo(entity.getTradeNo());
        tradeBillDetail.setPaidCapital(entity.getPaidCapital());
        tradeBillDetail.setDiscountCapital(entity.getDiscountCapital());
        tradeBillDetail.setPaidChannel(PaymentChannelEnum.getEnumByCode(Integer.valueOf(entity.getPaidChannel())));
        tradeBillDetail.setBankNo(entity.getBankNo());
        tradeBillDetail.setPayBizNo(entity.getPayBizNo());
        tradeBillDetail.setChannelServiceFee(entity.getChannelServiceFee());

        return tradeBillDetail;
    }
}
